/*
 * ****************************************************************************
 * Copyright: 2020-2030, YZChain Tech. Co., Ltd.
 * File name:
 * Description:
 * Author: Dc luo
 * Version: v1.0
 * Date: $DATE
 * History:
 * ***************************************************************************
 */

package com.srp.blockchain.bcos.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

@Slf4j
public class AbiUtils {

    public static final String CONTRACT_API_PATH = "contract.abi/";

    public static String getContractABI(String contract) {
        log.info("get contract api: [{}]", contract);
        InputStream fis = null;
        String str = null;
        try {
            ClassPathResource classPathResource = new ClassPathResource(CONTRACT_API_PATH + contract + ".abi");
            fis = classPathResource.getInputStream();

            byte[] buff = new byte[fis.available()];
            int size = fis.read(buff);
            if (size > 0) {
                str = new String(buff, StandardCharsets.UTF_8);
            }
        } catch (FileNotFoundException e) {
            log.error("getDataByPath error", e);
        } catch (IOException e) {
            log.error("getDataByPath error", e);
        } finally {
            if (null != fis) {
                try {
                    fis.close();
                } catch (IOException e) {
                    log.error("getDataByPath error", e);
                }
            }
        }
        return str;
    }

}
